using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameHanhTrinh.FrameWork
{
    class CSprite
    {
        private ResourceId _id;
        private Texture2D _texture;
        private Vector2 _pos;
        private int _numCols;
        private int _numRows;
        private int _frameWidth;
        private int _frameHeight;
        private int _curCols;
        private int _curRows;

        private Vector2 _origin;
        private float _depth;
        private Vector2 _scale;
        private float _rotate;
        private SpriteEffects _effect;
        private Color _color;

        private float _timeAnimation;
        private float _localTime;

        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }
        public float TimeAnimation
        {
            get { return _timeAnimation; }
            set { _timeAnimation = value; }
        }
        public int Width
        {
            get { return _frameWidth; }
        }
        public int Height
        {
            get { return _frameHeight; }
        }

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public CSprite(ResourceId id, Texture2D texture, Vector2 pos, int numCols, int numRows, int frameWidth, int frameHeight, float timeAnimation)
        {
            _id = id;
            _texture = texture;
            _pos = pos;
            _numCols = numCols;
            _numRows = numRows;
            _frameHeight = frameHeight;
            _frameWidth = frameWidth;

            _curCols = 0;
            _curRows = 0;
            _origin = Vector2.Zero;
            _depth = 1.0f;
            _scale = new Vector2(1.0f, 1.0f);
            _rotate = 0.0f;
            _effect = SpriteEffects.None;
            _color = Color.White;
            _timeAnimation = timeAnimation;
            _localTime = 0.0f;
        }

        public void Update(GameTime gametime)
        {
            if (_timeAnimation == 0)
                return;
            _localTime += (float)gametime.ElapsedGameTime.TotalMilliseconds;
            if (_localTime > _timeAnimation)
            {
                NextFrame();
                _localTime = 0;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, _pos, new Rectangle(_curCols * _frameWidth, _curRows * _frameHeight, _frameWidth, _frameHeight), _color, _rotate, _origin, _scale, _effect, _depth);
        }


        public Rectangle GetBoundingBox()
        {
            Rectangle aRect = new Rectangle();

            aRect.X = (int)_pos.X;
            aRect.Y = (int)_pos.Y;
            aRect.Width = (int)(_frameWidth * _scale.X);
            aRect.Height = (int)(_frameHeight * _scale.Y);

            return aRect;
        }

        public void NextFrame()
        {
            _curCols++;
            if (_curCols >= _numCols)
            {
                _curRows++;
                _curCols = 0;
            }
            if (_curRows >= _numRows)
                _curRows = 0;
        }

        public void Reset()
        {
            _curCols = _curRows = 0;
        }
    }
}
